package scct.jiangjiale.dao;

import scct.jiangjiale.model.User;
import scct.jiangjiale.utility.dbUtilis;

import java.sql.Connection;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDao {
    /**
     * @param name 用户名
     * @param pass 密码
     * @return 返回登陆的用户对象
     */


    public User login(String name, String pass) {
        User user = null;
        try {
            Connection connection = dbUtilis.getConnection();
            String sql = "select * from db01 where name=? and password=?";
            PreparedStatement pstmt = connection.prepareStatement(sql);
            pstmt.setString(1, name);
            pstmt.setString(2, pass);

            ResultSet resultSet = pstmt.executeQuery();
            if (resultSet.next()) {
                int id = resultSet.getInt("id");
                String username = resultSet.getString("name");
                String password = resultSet.getString("password");
                int role = resultSet.getInt("role");
                String sex = resultSet.getString("sex");
                String mobile = resultSet.getString("mobile");
                int age = resultSet.getInt("age");
                user = new User(id, name, password, role, sex, mobile, age);

            }
            dbUtilis.closeConnection(connection);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return user;
    }
    //..其他操作方法


    public List<User> getAllUser() {
        Connection conn = dbUtilis.getConnection();
        List<User> users = new ArrayList<>();
        String sql = "select * from db01";

        try {
            PreparedStatement pstmt = conn.prepareStatement(sql);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                int id = rs.getInt("id");
                String name = rs.getString("name");
                String password = rs.getString("password");
                int role = rs.getInt("role");
                String sex = rs.getString("sex");
                String mobile = rs.getString("mobile");
                int age = rs.getInt("age");
                users.add(new User(id, name, password, role, sex, mobile, age));

            }
            dbUtilis.closeConnection(conn);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }


        return users;
    }

    public boolean addUser(String name, String password, String sex, int age, String mobile) throws SQLException {
        int result = 0;
        Connection conn = dbUtilis.getConnection();
        String insert_sql = "insert into db01 values(?,?,?,?,null,null,?)";
        PreparedStatement pstmt = conn.prepareStatement(insert_sql);
        pstmt.setString(1, name);
        pstmt.setString(2, password);
        pstmt.setInt(3, age);
        pstmt.setString(5,mobile);
        pstmt.setString(4, sex);
        result+=pstmt.executeUpdate();
        return result==1;
    }


}


